package com.huiminpay.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.domain.BusinessException;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.util.RandomUuidUtil;
import com.huiminpay.merchant.api.AppService;
import com.huiminpay.merchant.convert.AppCovert;
import com.huiminpay.merchant.dto.AppDTO;
import com.huiminpay.merchant.entity.App;
import com.huiminpay.merchant.entity.Merchant;
import com.huiminpay.merchant.mapper.AppMapper;
import com.huiminpay.merchant.mapper.MerchantMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @ClassName AppServiceImpl
 * @Author: longlong
 * @CreateDate: 2023/12/6 10:33
 */

@Service
public class AppServiceImpl implements AppService {

    @Autowired
    MerchantMapper merchantMapper;
    @Autowired
    AppMapper appMapper;

    @Transactional
    @Override
    public AppDTO createApp(Long merchantId, AppDTO dto) throws BusinessException {
        //检验用户是否通过资质审核
        Merchant merchant = merchantMapper.selectById(merchantId);
        if (ObjectUtils.isEmpty(merchant)) {
            throw new BusinessException(CommonErrorCode.E_200002);
        }
        if (!"2".equals(merchant.getAuditStatus())) {  //2 为审核通过状态  0 1 3其他
            throw new BusinessException(CommonErrorCode.E_200003);
        }
        //检验app名字是否存在
        LambdaQueryWrapper<App> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(App::getAppName, dto.getAppName());
        Integer count = appMapper.selectCount(wrapper);
        if (count > 0) {
            throw new BusinessException(CommonErrorCode.E_200004);
        }
        App entity = AppCovert.INSTANCE.dto2entity(dto);
        entity.setMerchantId(merchantId);
        entity.setAppId(RandomUuidUtil.getUUID());
        appMapper.insert(entity);

        return AppCovert.INSTANCE.entity2dto(entity);
    }

    @Override
    public List<AppDTO> queryAppByMerchant(Long merchantId) throws BusinessException {
        List<App> apps = appMapper.selectList(new LambdaQueryWrapper<App>().eq(App::getMerchantId, merchantId));
        return AppCovert.INSTANCE.listEntity2dto(apps);
    }

    /**
     * 根据业务 app_id 查询应用
     *
     * @param id
     * @return com.huiminpay.merchant.api.dto.AppDTO
     * @author qawine
     * @date 2021/3/24 20:28
     */
    @Override
    public AppDTO getAppById(String id) throws BusinessException {
        App app = appMapper.selectOne(new LambdaQueryWrapper<App>().eq(App::getAppId, id));
        return AppCovert.INSTANCE.entity2dto(app);
    }

    /**
     *  查询应用是否属于某个商户
     * @author glls
     * @param appId  应用id
     * @param merchantId  商户id
     * @return java.lang.Boolean
     * @date 2021/3/3 20:18
     */
    @Override
    public Boolean queryAppInMerchant(String appId, Long merchantId) {
        Integer count = appMapper.selectCount(new LambdaQueryWrapper<App>()
                .eq(App::getAppId, appId).eq(App::getMerchantId, merchantId));
        return count>0;
    }
}
